package ltd.newbee.mall.api.admin;

import ltd.newbee.mall.entity.Role;
import ltd.newbee.mall.entity.User;
import ltd.newbee.mall.service.impl.RoleService;
import ltd.newbee.mall.util.Result;
import ltd.newbee.mall.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manage-api/v1/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        try {
            List<Role> roles = roleService.getRoleList(params);
            return ResultGenerator.genSuccessResult(roles);
        } catch (Exception e) {
            return ResultGenerator.genFailResult("获取角色列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取角色菜单权限
     */
    @GetMapping("/{roleId}/menus")
    public Result getRoleMenus(@PathVariable Long roleId) {
        try {
            List<Long> menuIds = roleService.getRoleMenuIds(roleId);
            return ResultGenerator.genSuccessResult(menuIds);
        } catch (Exception e) {
            return ResultGenerator.genFailResult("获取角色菜单权限失败: " + e.getMessage());
        }
    }

    /**
     * 更新角色菜单权限
     */
    @PostMapping("/{roleId}/updateMenus")
    public Result updateRoleMenus(@PathVariable Long roleId, @RequestBody List<Long> menuIds) {
        try {
            boolean success = roleService.updateRoleMenus(roleId, menuIds);
            return ResultGenerator.genSuccessResult("权限更新成功");
        } catch (Exception e) {
            return ResultGenerator.genFailResult("更新角色菜单权限失败: " + e.getMessage());
        }
    }

    @GetMapping("/{roleId}")
    public Result getRole(@PathVariable Long roleId) {
        try {
            Role role = roleService.getRoleById(roleId);
            return ResultGenerator.genSuccessResult(role);
        } catch (Exception e) {
            return ResultGenerator.genFailResult("获取角色信息失败: " + e.getMessage());
        }
    }

    @PostMapping
    public Result add(@RequestBody Role role) {
        try {
            boolean success = roleService.addRole(role);
            return success ? ResultGenerator.genSuccessResult("添加成功") :
                    ResultGenerator.genFailResult("添加失败");
        } catch (Exception e) {
            return ResultGenerator.genFailResult("添加角色失败: " + e.getMessage());
        }
    }

    @PutMapping
    public Result update(@RequestBody Role role) {
        try {
            boolean success = roleService.updateRole(role);
            return success ? ResultGenerator.genSuccessResult("更新成功") :
                    ResultGenerator.genFailResult("更新失败");
        } catch (Exception e) {
            return ResultGenerator.genFailResult("更新角色失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{roleId}")
    public Result delete(@PathVariable Long roleId) {
        try {
            boolean success = roleService.deleteRole(roleId);
            return success ? ResultGenerator.genSuccessResult("删除成功") :
                    ResultGenerator.genFailResult("删除失败");
        } catch (Exception e) {
            return ResultGenerator.genFailResult("删除角色失败: " + e.getMessage());
        }
    }

    @GetMapping("/{roleId}/users")
    public Result getRoleUsers(@PathVariable Long roleId) {
        try {
            List<User> users = roleService.getUsersByRoleId(roleId);
            return ResultGenerator.genSuccessResult(users);
        } catch (Exception e) {
            return ResultGenerator.genFailResult("获取角色用户失败: " + e.getMessage());
        }
    }
}
